package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import logic.Destination;
import logic.Product;
import logic.ShoppingKart;
import logic.User;

public class JDBCValidateDAO implements IValidateDAO {
	
		private Connection c;
		IProductDAO pdao;
		IUserDAO udao;
		IDestinationDAO ddao;
		String idDest;
		
		public JDBCValidateDAO() {
		       c = ConnectionManager.getInstance().checkOut();
		       pdao = new JDBCProductDAO();
		       ddao = new JDBCDestinationDAO();
		       udao = new JDBCUserDAO();
		}
		
		protected void finalize() {
		       ConnectionManager.getInstance().checkIn(c);
		}

		/**public User startDonate(Product p,String idUser, String idDestination) {
			
			User u = udao.select(idUser);
			Destination d = ddao.select(idDestination);
			u.setAmountDonate(pdao.amountDonate(conn, idUser));
			u.setAmountDonateBuyed(pdao.amountDonatedPurchased(conn, idUser));
			u.setAmountpdao.moneyRaised(conn, idUser);
			
			PreparedStatement stmt = null;
			String sql = ("INSERT INTO purchasehistory ( iduser, totalproducts, totalprice, date) VALUES (?, ?, ?, ?)");
			
	        try {
	            stmt = c.prepareStatement(sql);
	            stmt.setString(1, s.getIdUser());
	            stmt.setLong(2, s.getTotalProducts());
	            stmt.setFloat(3, s.getTotalPrice());
	            stmt.setLong(4, s.getDate().getTime());
	            stmt.executeUpdate();

		        } catch (SQLException e) {
		            System.out.println("Message: " + e.getMessage());
		            System.out.println("SQLState: " + e.getSQLState());
		            System.out.println("ErrorCode: " + e.getErrorCode());
		        } finally {
	            try {
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
		    }
		}
		
		**/
		//Tirso
		public ShoppingKart selectPurchase(String idShoppingKart) {
			PreparedStatement stmt = null;
			String sql = "SELECT * FROM purchases WHERE id = ?";
			ShoppingKart s = null;
			ResultSet result = null;
			try{
				stmt = c.prepareStatement(sql);
				stmt.setString(1, idShoppingKart);
				result = stmt.executeQuery();
				result.next();
				s.setId(idShoppingKart);
				s.setIdDestination(result.getString("destination"));
				s.setDate(result.getDate("date"));
				s.setIdUser(result.getString("user"));
				s.setTotalPrice(result.getFloat("price"));
				s.setTotalProducts(result.getInt("totalproducts"));
				//falta lista productos y ver idShoppingKart
				
			}catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	        try {
	            if (stmt != null) {
	                stmt.close();
	            }
	        } catch (SQLException e) {
	        	}
	        }
			return s;
		}

		public void deletePurchase(String idShoppingKart) {
			PreparedStatement stmt = null;
			String sql = "DELETE * FROM purchasehistory WHERE (id = ?)";
	        try {
	            stmt = c.prepareStatement(sql);
	            stmt.setString(1, idShoppingKart);
	            stmt.executeUpdate();

		        } catch (SQLException e) {
		            System.out.println("Message: " + e.getMessage());
		            System.out.println("SQLState: " + e.getSQLState());
		            System.out.println("ErrorCode: " + e.getErrorCode());
		        } finally {
	            try {
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
		    }
		}

		public User selectUser(String idUser) {
			// TODO Auto-generated method stub
			return null;
		}
			
	
		

}
